package org.webball.controller;


import org.newdawn.slick.Input;
import org.webball.base.interfaces.IController;
import org.webball.base.interfaces.IPitch;
import org.webball.pitch.Pitch;
import org.webball.player.Player;
import org.webball.tools.MKVector;

public class Controller implements IController{

	public Player checkedPlayer;
	public boolean checked = false;
	
	int team = 0;
	public Controller(){
		
	}
	@Override
	public void action(IPitch pitch, Input input) {
		
		MKVector mousePosition = new MKVector(input.getMouseX(), input.getMouseY());
       
        //faza taktyczna
        if(pitch.isTacticalPhase()) {   
                if(!checked) {
                        if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                                checkedPlayer = (Player) pitch.clickOnPlayer(mousePosition, team);
                                if(checkedPlayer != null) {
                                        checked = true;
                                }
                        }
                }
                if(checked) {
                        if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && !input.isKeyDown(Input.KEY_SPACE)) {
                                checkedPlayer.setPosition(mousePosition);
                        }
                        else if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && input.isKeyDown(Input.KEY_SPACE)) {
                                checkedPlayer.setHitTarget(mousePosition);
                        }                               
                }
                        
        }
        //normalna faza
        else {
                if(!checked) {
                        if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                                checkedPlayer = (Player) pitch.clickOnPlayer(mousePosition, team);
                                if(checkedPlayer != null) {
                                        checked = true;
                                }
                        }
                }
                if(checked)
                        if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && !input.isKeyDown(Input.KEY_SPACE)) {
                                checkedPlayer.setMovementTarget(mousePosition);
                        }
                        else if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && input.isKeyDown(Input.KEY_SPACE)) {
                                checkedPlayer.setHitTarget(mousePosition);
                        }
        }
        if(input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {
                checked = false;
                pitch.deselectAllPlayers(team);
        }		
		
	}

	@Override
	public void setTeam(int team) {
		if(team == 0 || team == 1){
			this.team = team;
		}

	}

	

}
